package com.sf.guava.basic;

import com.google.common.collect.Ordering;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Classname OrderingTest
 * @Description 排序
 * @Date 2019/12/6 0006 18:27
 * @Created by 埔枘
 */
public class OrderingTest {

    public static void main(String[] args) {


    }

    private static void addList(List<Integer> numbers) {
        numbers.add(new Integer(5));
        numbers.add(new Integer(2));
        numbers.add(new Integer(15));
        numbers.add(new Integer(51));
        numbers.add(new Integer(53));
        numbers.add(new Integer(35));
        numbers.add(new Integer(45));
        numbers.add(new Integer(32));
        numbers.add(new Integer(43));
        numbers.add(new Integer(16));
    }

    //自然排序
    public static void  natural(){
        List<Integer> numbers = new ArrayList<Integer>();
        addList(numbers);
        //自然排序
        Ordering ordering = Ordering.natural();
        System.out.println("Input List: ");
        System.out.println(numbers);

        Collections.sort(numbers,ordering );
        System.out.println("Sorted List: ");
        System.out.println(numbers);
    }


    //是否已经排序
    public static void isOrdered(){
        List<Integer> numbers = new ArrayList<Integer>();
        Ordering ordering = Ordering.natural();
        addList(numbers);
        //判断是否已经排序完成
        System.out.println("List is sorted: " + ordering.isOrdered(numbers));
    }
    //获取最值
    public static void maxOrMin(){
        List<Integer> numbers = new ArrayList<Integer>();
        Ordering ordering = Ordering.natural();
        System.out.println("Minimum: " + ordering.min(numbers));
    }
    //反序
    public static void reverse(){
        List<Integer> numbers = new ArrayList<Integer>();
        Ordering ordering = Ordering.natural();
        Collections.sort(numbers,ordering.reverse());
        System.out.println("Reverse: " + numbers);
    }
    //把 null 排到第一个
    public static void firstNull(){
        List<Integer> numbers = new ArrayList<Integer>();
        Ordering ordering = Ordering.natural();
        Collections.sort(numbers,ordering.nullsFirst());
        System.out.println("Null first Sorted List: ");
        System.out.println(numbers);
    }
}
